package com.javasm.controller;

import com.alibaba.fastjson.JSON;
import com.javasm.entity.PageInfo;
import com.javasm.entity.ReturnCodeEnum;
import com.javasm.entity.ReturnResult;
import com.javasm.entity.SkillGameServer;
import com.javasm.service.SkillGameServerService;
import com.javasm.service.impl.SkillGameServerServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * 技能区服(CustomerSkillGameServer)表控制层
 *
 */
@WebServlet("/gameServer/*")
public class GameServerServlet extends BaseServlet {

    public void query(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1接收请求参数
        String pageStr = req.getParameter("page");
        String pageSizeStr = req.getParameter("pageSize");
        String gameIdStr = req.getParameter("gameId");




        //页码默认值
        Integer page = 1;
        Integer pageSize = 10;

        Integer gameId = 0;

        //如果页面传值了 就用页面的值
        if(pageStr!=null&&!"".equals(pageStr)){
            page = Integer.parseInt(pageStr);
        }
        if(pageSizeStr!=null&&!"".equals(pageSizeStr)){
            pageSize = Integer.parseInt(pageSizeStr);
        }
        if(gameIdStr!=null&&!"".equals(gameIdStr)){
            gameId = Integer.parseInt(gameIdStr);
        }

        //根据页面传来的查询参数
        //构建查询语句使用的查询条件对象
        SkillGameServer inputGame = new SkillGameServer();
        inputGame.setGameId(gameId);

        //2调用service
        SkillGameServerService ms = new SkillGameServerServiceImpl();

        Integer total = ms.countGameServers(inputGame);
        //总页数计算公式 total%pageSize 能不能整除
        //能整除    total/pageSize   页
        //不能整除  total/pageSize+1  页
        Integer totalPage ;
        totalPage = total%pageSize==0?total/pageSize:total/pageSize+1;
        //检查当前要查的页数 是否超过了最大页数
        //如果超过了 则用最后一页查
        //修复bug时 有可能产生新bug
        //功能完成后 需要重新测试 之前测通的功能 可能会与现在的功能产生冲突 造成bug
        if(page!=1){
            if(page>totalPage){
                page = totalPage;
            }
        }


        List<SkillGameServer> customerSkillGames = ms.listAllGameServer(inputGame, page, pageSize);

        PageInfo returnPageInfo = new PageInfo(page,pageSize,total);
        //3反馈操作结果
        ReturnResult rrs = new ReturnResult();
        if(customerSkillGames.size()>0){
            rrs.setCode(ReturnCodeEnum.QUERY_SUCCESS.getCode());
            rrs.setMsg(ReturnCodeEnum.QUERY_SUCCESS.getMsg());

            rrs.setPageInfo(returnPageInfo);
            rrs.setReturnData(customerSkillGames);
        }else{
            rrs.setCode(ReturnCodeEnum.QUERY_NODATA.getCode());
            rrs.setMsg(ReturnCodeEnum.QUERY_NODATA.getMsg());
        }
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.print(JSON.toJSONString(rrs));
        writer.close();
    }
    public void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String gameServerIdStr = req.getParameter("gameServerId");
        Integer gameServerId = null;
        if(gameServerIdStr!=null&&!"".equals(gameServerIdStr)){
            gameServerId = Integer.parseInt(gameServerIdStr);
        }
        String gameServerName = req.getParameter("gameServerName");
        String gameIdStr = req.getParameter("gameId");
        Integer gameId = null;
        if(gameIdStr!=null&&!"".equals(gameIdStr)){
            gameId = Integer.parseInt(gameIdStr);
        }
        SkillGameServer inputGame = new SkillGameServer(gameServerId,gameServerName,gameId);
        SkillGameServerService ms = new SkillGameServerServiceImpl();
        Integer result = ms.addGameServer(inputGame);
        ReturnResult rrs = null;
        if(result>0){
            rrs = new ReturnResult(ReturnCodeEnum.DATA_OPERATION_SUCCESS.getCode(),ReturnCodeEnum.DATA_OPERATION_SUCCESS.getMsg());

        }else{
            rrs = new ReturnResult(
                    ReturnCodeEnum.DATA_OPERATION_FAILED.getCode(),
                    ReturnCodeEnum.DATA_OPERATION_FAILED.getMsg()

            );
        }
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.print(JSON.toJSONString(rrs));
        writer.close();
    }
    public void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String gameServerIdStr = req.getParameter("gameServerId");
        Integer gameServerId = null;
        if(gameServerIdStr!=null&&!"".equals(gameServerIdStr)){
            gameServerId = Integer.parseInt(gameServerIdStr);
        }
        SkillGameServerService ms = new SkillGameServerServiceImpl();
        Integer result = ms.deleteGameServerById(gameServerId);
        ReturnResult rrs = null;
        if(result>0){
            rrs = new ReturnResult(ReturnCodeEnum.DATA_OPERATION_SUCCESS.getCode(),ReturnCodeEnum.DATA_OPERATION_SUCCESS.getMsg());

        }else{
            rrs = new ReturnResult(
                    ReturnCodeEnum.DATA_OPERATION_FAILED.getCode(),
                    ReturnCodeEnum.DATA_OPERATION_FAILED.getMsg()
            );
        }
        resp.setContentType("application/json;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.print(JSON.toJSONString(rrs));
        writer.close();
    }

}


